package com.yuke.cloud.service.wmc.controller.frontend;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yuke.cloud.common.core.annotation.LogAnnotation;
import com.yuke.cloud.common.core.support.BaseController;
import com.yuke.cloud.common.util.wrapper.WrapMapper;
import com.yuke.cloud.common.util.wrapper.Wrapper;
import com.yuke.cloud.service.wmc.dto.FlowCountDto;
import com.yuke.cloud.service.wmc.dto.PaymentOrderDto;
import com.yuke.cloud.service.wmc.dto.WmsPurchaseOrderDto;
import com.yuke.cloud.service.wmc.dto.WmsStockInPurchaseDto;
import com.yuke.cloud.service.wmc.entity.WmsPurchaseOrder;
import com.yuke.cloud.service.wmc.service.WmsFlowService;
import com.yuke.cloud.service.wmc.service.WmsPurchaseOrderService;
import com.yuke.cloud.service.wmc.service.WmsStockInService;
import com.yuke.cloud.service.wmc.service.WmsStorageRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;

/**
* Created by zn on 2020/10/08.
*/
@Api(value = "WEB - WmsPurchaseOrderController", tags = "处理WmsPurchaseOrder相关请求", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@RestController
@RequestMapping("/wms/purchase_order")
public class WmsPurchaseOrderController extends BaseController {
    @Resource
    private WmsPurchaseOrderService wmsPurchaseOrderService;
    @Resource
    private WmsStockInService wmsStockInService;
    @Resource
    private WmsStorageRecordService wmsStorageRecordService;
    @Resource
    private WmsFlowService wmsFlowService;

    /**
     * 1.存入进货单
     * 2.存入入库表 wms_stock_in 入库表
     * 3.需要更新二张表
     * wms_storage库存商品表、wms_storage_recorder库存记录表
     */
    @PostMapping
    @SneakyThrows
    @LogAnnotation
    @ApiOperation(httpMethod = "POST", value = "新增一条记录")
    public Wrapper add(@ApiParam(name = "wmsPurchaseOrder", value = "该数据记录的POJO") @RequestBody WmsPurchaseOrderDto wmsPurchaseOrderDto) {
        //Long updateId = getLoginAuthDto().getUserId();
        //wmsPurchaseOrderDto.setUpdateId(updateId);
        WmsPurchaseOrder wmsPurchaseOrder = new WmsPurchaseOrder();
        BeanUtils.copyProperties(wmsPurchaseOrderDto,wmsPurchaseOrder);
        wmsPurchaseOrder.setPurchaseId(generateId());
        //订单编号
        //20位长度的字符串
        java.util.UUID id = UUID.randomUUID();
        String[] idd = id.toString().split("-");
        String uuid20 = idd[0] + idd[1] + idd[2] + idd[3];
        //12位长度,相加一共生成32位的订单号
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYYMMddHHmm");
        String format = simpleDateFormat.format(new Date());
        wmsPurchaseOrder.setPurchaseSn(format+uuid20);
        //存入进货单
        int save = wmsPurchaseOrderService.save(wmsPurchaseOrder);
        if (save==1){
  /*          //if (wmsPurchaseOrder.getPayable()!=null) {
                SupplierApiDto supplierApiDto = new SupplierApiDto();
                supplierApiDto.setSupplierId(wmsPurchaseOrder.getSupplierId());
                supplierApiDto.setAmountPaid(wmsPurchaseOrder.getPayment());
                supplierApiDto.setAmountPayable(wmsPurchaseOrder.getPayable());
                //修改供应商的实付应付
                Integer integer = uacUserFeignApi.updateSupplierById(supplierApiDto);
                System.out.println("supplierApiDto" + supplierApiDto);*/
                //商品入库
            wmsPurchaseOrderDto.setPurchaseId(wmsPurchaseOrder.getPurchaseId());
            //需要更新三张表
            //  wms_stock_in 入库表、wms_storage库存商品表、wms_storage_recorder库存记录表
            CompletableFuture<Integer> num = wmsStockInService.insertWmsDto(wmsPurchaseOrderDto);
            CompletableFuture<Integer> goodsStorage = wmsStockInService.updateWmsStorage(wmsPurchaseOrderDto);
            CompletableFuture<Integer> recorder = wmsStorageRecordService.updateWmsStorageRecorder(wmsPurchaseOrderDto);

            // 等待所有任务都执行完
            CompletableFuture.allOf(num, recorder, goodsStorage).join();
            // 获取每个任务的返回结果
            Integer result = recorder.get() + goodsStorage.get()+ num.get();
            return WrapMapper.ok(result);
            }
        return WrapMapper.ok(save);
    }

    //存入报溢单
    @PostMapping("/addOverFlow")
    @ApiOperation(httpMethod = "POST", value = "新增一条记录")
    public Wrapper addOverFlow(@ApiParam(name = "wmsPurchaseOrder", value = "该数据记录的POJO") @RequestBody WmsPurchaseOrderDto wmsPurchaseOrderDto) {
        WmsPurchaseOrder wmsPurchaseOrder = new WmsPurchaseOrder();
        BeanUtils.copyProperties(wmsPurchaseOrderDto,wmsPurchaseOrder);
        wmsPurchaseOrder.setPurchaseId(generateId());
        //订单编号
        //20位长度的字符串
        java.util.UUID id = UUID.randomUUID();
        String[] idd = id.toString().split("-");
        String uuid20 = idd[0] + idd[1] + idd[2] + idd[3];
        //12位长度,相加一共生成32位的订单号
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYYMMddHHmm");
        String format = simpleDateFormat.format(new Date());
        wmsPurchaseOrder.setPurchaseSn(format+uuid20);
        //存入进货单
        int save = wmsPurchaseOrderService.save(wmsPurchaseOrder);
        if (save==1){
                //商品入库
                wmsPurchaseOrderDto.setPurchaseId(wmsPurchaseOrder.getPurchaseId());
                Integer num = wmsStockInService.addOverFlow(wmsPurchaseOrderDto);
                return WrapMapper.ok(num);
        }
        return WrapMapper.ok(save);
    }

    @GetMapping("/deleteStorageByPurchaseOrder")
    @ApiOperation(httpMethod = "GET", value = "根据id删除一条记录")
    public Wrapper deleteStorageByPurchaseOrder(@ApiParam(name = "id", value = "记录id") @RequestParam Long Id) {
        System.out.println();
        //修改进货单
        WmsPurchaseOrder wmsPurchaseOrder = new WmsPurchaseOrder();
        wmsPurchaseOrder.setPurchaseId(Id);
        wmsPurchaseOrder.setIsDeleted((byte) 1);
        Integer update = wmsPurchaseOrderService.update(wmsPurchaseOrder);
        if (update>0){
            //修改库存
            Integer num =  wmsPurchaseOrderService.deleteStorage(Id);
            //暂不修改流水
 /*           if (num>0){
                //修改流水
                WmsFlow wmsFlow = new WmsFlow();
                wmsFlow.setPurchaseId(Id);
                wmsFlow.setFlowIsDeleted((byte)1);
                update = wmsFlowService.update(wmsFlow);
                return WrapMapper.ok(update);
            }*/
            return WrapMapper.ok(num);
        }
        return WrapMapper.ok(update);
    }


    @DeleteMapping("/{id}")
    @ApiOperation(httpMethod = "DELETE", value = "根据id删除一条记录")
    public Wrapper delete(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        wmsPurchaseOrderService.deleteById(id);
        return WrapMapper.ok();
    }

    @PutMapping
    @ApiOperation(httpMethod = "PUT", value = "修改一条记录")
    public Wrapper update(@ApiParam(name = "wmsPurchaseOrder", value = "需要修改的内容") @RequestBody WmsPurchaseOrder wmsPurchaseOrder) {
        wmsPurchaseOrderService.update(wmsPurchaseOrder);
        return WrapMapper.ok();
    }

    @GetMapping("/{id}")
    @ApiOperation(httpMethod = "GET", value = "根据ID获取具体信息")
    public Wrapper<WmsPurchaseOrder> detail(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        WmsPurchaseOrder wmsPurchaseOrder = wmsPurchaseOrderService.findById(id);
        return WrapMapper.ok(wmsPurchaseOrder);
    }

    @GetMapping
    @ApiOperation(httpMethod = "GET", value = "分页获取所有记录")
    public Wrapper<PageInfo<WmsPurchaseOrder>> list(@ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                    @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<WmsPurchaseOrder> list = wmsPurchaseOrderService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return WrapMapper.ok(pageInfo);
    }


    @PostMapping("/selectCondition")
    @ApiOperation(httpMethod = "POST", value = "条件查询")
    public Wrapper<PageInfo<WmsPurchaseOrderDto>> selectCondition(@ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                    @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size,
                                                     @RequestBody WmsPurchaseOrderDto wmsPurchaseOrderDto
    ){
        PageHelper.startPage(page, size);
        List<WmsPurchaseOrderDto> list = wmsPurchaseOrderService.selectCondition(wmsPurchaseOrderDto);
        PageInfo pageInfo = new PageInfo(list);
        return WrapMapper.ok(pageInfo);
    }

    @GetMapping("/findGoodsByPurchaseId")
    @ApiOperation(httpMethod = "POST", value = "根据purchaseId查找商品")
    public Wrapper<PageInfo<WmsStockInPurchaseDto>> findGoodsByPurchase(@ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                                        @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size,
                                                                        @ApiParam(name = "size", value = "purchaseId", defaultValue = "0") @RequestParam Long purchaseId
    ){
        PageHelper.startPage(page, size);
        List<WmsStockInPurchaseDto> list = wmsPurchaseOrderService.findGoodsByPurchase(purchaseId);
        PageInfo pageInfo = new PageInfo(list);
        return WrapMapper.ok(pageInfo);
    }

    @GetMapping("/listAndOther")
    @ApiOperation(httpMethod = "GET", value = "分页获取所有记录包括其他")
    public Wrapper<PageInfo<WmsPurchaseOrderDto>> listAndOther(@ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                    @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size
    ){
        PageHelper.startPage(page, size);
        List<WmsPurchaseOrderDto> list = wmsPurchaseOrderService.listAndOther();
        PageInfo pageInfo = new PageInfo(list);
        return WrapMapper.ok(pageInfo);
    }

    //新建表,暂时不用
    //统计付款单
    @GetMapping("/findAllPayment")
    @ApiOperation(httpMethod = "GET", value = "统计付款单")
    public Wrapper<PageInfo<WmsPurchaseOrderDto>> findAllPayment(@ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                               @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size,
                                                               @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") PaymentOrderDto paymentOrderDto


    ){
        PageHelper.startPage(page, size);
        List<PaymentOrderDto> list = wmsPurchaseOrderService.findAllPayment(paymentOrderDto);
        PageInfo pageInfo = new PageInfo(list);
        return WrapMapper.ok(pageInfo);
    }


    /*统计某商家 某时间 往来账 的统计
      即,,进货单的实付,应付
        进货退货单的实付,应付
     */
    @PostMapping("/countMoney")
    @ApiOperation(httpMethod = "POST", value = "统计实付应付")
    public Wrapper<PageInfo<FlowCountDto>> countMoney(@ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                      @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size,
                                                      @RequestBody WmsPurchaseOrderDto wmsPurchaseOrderDto
    ){
        PageHelper.startPage(page, size);
        List<FlowCountDto> list = wmsPurchaseOrderService.countMoney(wmsPurchaseOrderDto);
        PageInfo pageInfo = new PageInfo(list);
        return WrapMapper.ok(pageInfo);
    }


}
